//
//  WWStatusCell.swift
//  SinWEiBo
//
//  Created by 胡良志 on 2018/3/29.
//  Copyright © 2018年 HuLiangZhi. All rights reserved.
//

import UIKit
import SDWebImage
import YYText
let margin : CGFloat = 8 //图片外间距
let imageMargin : CGFloat = 2 //图片的间距
let maxWidth = ScreenWidth - 2 * margin //视图总宽度
let itemWidth = (maxWidth - 2 * imageMargin) / 3 //每个图片的宽度

//var imageSize : CGSize = CGSize()

class WWStatusCell: UITableViewCell {
    
    //MARK: 微博头像
    @IBOutlet weak var iconImage: UIImageView!
    //MARK: 微博名称
    @IBOutlet weak var nameLabel: UILabel!
    //MARK: 微博时间
    @IBOutlet weak var timeLabel: UILabel!
    //MARK: 微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    //MARK: 微博正文
    @IBOutlet weak var contentLabel: YYLabel!
    //用户认证类型
    @IBOutlet weak var avatarView: UIImageView!
    //用户等级
    @IBOutlet weak var mbRankView: UIImageView!
    //配图视图
    @IBOutlet weak var pictureview: WWPictureView!
    //布局对象
    @IBOutlet weak var pictureViewFlowLayout: UICollectionViewFlowLayout!
    //视图宽度
    @IBOutlet weak var pictureWidth: NSLayoutConstraint!
    //视图高度
    @IBOutlet weak var pictureHeight: NSLayoutConstraint!
    //转发微博
    @IBOutlet weak var retweetedLabel: UILabel!
    
    
    var viewModel : WWStatusViewModel?{
        didSet{
            contentLabel.attributedText = viewModel?.originalAttributedString
            //转发微博数据j
            retweetedLabel?.attributedText = viewModel?.retWeetedAttributeString
            nameLabel.text = viewModel?.status?.user?.name
            timeLabel.text = viewModel?.timeText
            sourceLabel.text = viewModel?.sourceText
            iconImage.sd_setImage(with: viewModel?.iconURL, completed: nil)
            avatarView.image = viewModel?.approveImage
            mbRankView.image = viewModel?.mbRankImage
            //给配图视图图片赋值
            pictureview.pictureInfos = viewModel?.imageURLs
            //拿到图片张数
            let count = viewModel?.imageURLs?.count ?? 0
            //得到图片张数返回视图宽高
            let psize = self.calculatePictureViewSize(count: count)
            
            pictureWidth.constant = psize.baseItem.width
            pictureHeight.constant = psize.baseItem.height
            pictureViewFlowLayout.itemSize = psize.itemSize

            iconImage.layer.cornerRadius = iconImage.bounds.width * 0.5
            iconImage.layer.masksToBounds = true
            //布局好子控件之后让约束立即生效,不然快速拖动界面图片可能显示不出来
            pictureview.layoutIfNeeded()
            //点击高亮的闭包
//            print("--",viewModel?.status?.id)
            contentLabel.highlightTapAction = { (containerView,text,range,rect) -> () in
                //containerView,就是被点击的视图
                //text: 被点击的属性文本
                //range: 点击的特殊文本 范围
                //rect: 点击的特殊的文本的坐标
                let sub = (text.string as NSString).substring(with: range)
                //跳转页面   响应者链条来实现页面跳转
                //当用户点击的是url的时候 就执行跳转页面 显示对应的网页
                //判断如果是 http开头的就认为是网页
                if sub.hasPrefix("http") {
                    print("http")
                    let webVC = WebViewController()
                    webVC.urlString = sub
                   self.findNavController()?.pushViewController(webVC, animated: true)
                }
            }
            
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentLabel.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * margin
        self.retweetedLabel?.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * margin

        //设置流水布局
        pictureViewFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureViewFlowLayout.minimumLineSpacing = imageMargin
        pictureViewFlowLayout.minimumInteritemSpacing = imageMargin
        self.contentLabel.numberOfLines = 0
        
    }

    //根据配图视图的张数计算配图视图的大小,并更新视图的宽高约束
    private func calculatePictureViewSize (count : Int) -> (baseItem:CGSize,itemSize:CGSize){
        if count == 0 {
            return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }

        if count == 1 {
            if let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: viewModel?.imageURLs?.last?.wap360 ?? "") {
                let imageSize = image.size
                //不能再if中单独设置itemSize
                //pictureViewFlowLayout.itemSize = imageSize
                return (imageSize,imageSize)
            }
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
        }
        if count == 4 {
            let width = 2 * itemWidth + margin
            return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))
        }
        
        //其他多张图片需要确定行数
        /*
         1,2,3  1
         4,5,6  2
         7,8,9  3
         */
        //已知列数和总个数 需要确定行数
        let row = CGFloat((count - 1) / 3) + 1
        let h = row * itemWidth + (row - 1) * imageMargin
        return (CGSize(width: maxWidth, height: h),CGSize(width: itemWidth, height: itemWidth))
    }

    
    
}

